﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace Jy123.Web.Module.TCenter
{
    public class TCenterManage : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string strVId = context.Request.QueryString["VenuesId"];
            if (string.IsNullOrEmpty(strVId))
            {
                strVId = context.Request["VenuesId"];
            }
            string type = context.Request.QueryString["Type"];
            if (string.IsNullOrEmpty(type))
            {
                type = context.Request["Type"];
            }
            if (!string.IsNullOrEmpty(strVId))
            {
                BLL.TCenter_Main BLLTCenter = new Jy123.BLL.TCenter_Main();
                BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                Model.TCenter_Main TCenter = BLLTCenter.GetModel(int.Parse(strVId));
                string strRedirect = string.Format("/Module/TCenter/TrainInfo.aspx?TCenterId={0}&Time={1}", strVId, DateTime.Now.ToLongDateString());

                string originalId = context.Request.Form["selClubHidden"];
                int cId = 0;
                BLL.TCenter.TModule BLLTModule = new Jy123.BLL.TCenter.TModule();
                switch (type.ToLower())
                {
                    case "join":
                        cId = int.Parse(context.Request["ClubId"]);
                        Model.Club_Main club = BLLClub.GetModel(cId);
                        BLLTModule.JoinTCenter(club, TCenter);
                        context.Response.Redirect(strRedirect);
                        break;
                    case "start":
                        TCenter.TrainClub = originalId.Replace("_", ",");
                        BLLTCenter.Update(TCenter);
                        BLLTModule.StartTrain(TCenter);
                        context.Response.Redirect(strRedirect);
                        break;
                    case "quit":
                        string[] strClub = TCenter.TrainClub.Split(',');
                        string strMark = context.Request.QueryString["Mark"];
                        if (string.IsNullOrEmpty(strMark))
                        {
                            strMark = context.Request["Mark"];
                        }
                        int mark = int.Parse(strMark);
                        cId = int.Parse(strClub[mark]);
                        Model.Club_Main cClub = BLLClub.GetModel(cId);
                        BLLTModule.QuitTCenter(cClub, TCenter);
                        BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
                        Model.Public_JType JType = BLLJType.GetModel(cClub.FJType.Value);
                        bool isCar = (JType.FSportType.Value == Convert.ToInt32(BLL.Public.EnumConfig.SportType.Car) || JType.FSportType.Value == Convert.ToInt32(BLL.Public.EnumConfig.SportType.F1));
                        if (isCar)
                        {
                            strRedirect = string.Format("/Module/CarClub/MyClub.aspx?ClubId={0}", cId.ToString());
                        }
                        else
                        {
                            strRedirect = string.Format("/Module/Club/MyClub.aspx?ClubId={0}", cId.ToString());
                        }

                        context.Response.Redirect(strRedirect);
                        break;
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
